package router

import (
	handler "django_blog_gin/internal/api"
	accountApi "django_blog_gin/internal/api/account"
	blogApi "django_blog_gin/internal/api/blog"
	"django_blog_gin/internal/api/chat"
	"django_blog_gin/internal/middleware"
	"django_blog_gin/internal/svc"
	"time"

	wsApi "django_blog_gin/internal/api/ws"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

// 初始路由
func SetupRouter() (r *gin.Engine, s *svc.ServiceContext) {
	svc := svc.NewServiceContext()
	gin.SetMode(svc.Config.Dev.Mode)

	router := gin.Default()
	router.SetTrustedProxies([]string{"127.0.0.1", "localhost"})
	router.Use(cors.New(cors.Config{
		AllowOrigins: []string{
			"http://127.0.0.1:8080", "http://localhost:8080",
			"http://localhost:8004", "http://127.0.0.1:8004",
			"http://127.0.0.1:5174", "http://localhost:5174",
			"http://localhost:5173", "http://127.0.0.1:5173"},
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))
	router.OPTIONS("/*path", func(c *gin.Context) { // 处理所有 OPTIONS 请求
		c.Status(204)
	})

	route := router.Group("/api")
	route.Use(middleware.JWTMiddleware())

	// account
	accountHandler := accountApi.NewAccountHandler(svc)
	{
		route.POST("/login", accountHandler.Login)
		route.POST("/refresh", accountHandler.Refresh)
		route.POST("/check", accountHandler.Check)

		route.POST("/get_auth_group_list", accountHandler.GetAuthGroupList)
		route.POST("/get_user_detail", accountHandler.GetUserDetail)
	}

	// blog
	blogRoute := route.Group("/blog")
	blogHandler := blogApi.NewBlogHandler(svc)
	{
		blogRoute.POST("/import_md_file", blogHandler.ImportMdFile)
		blogRoute.POST("/article_list", blogHandler.ArticleList)
		blogRoute.POST("/article_with_tags_list", blogHandler.GetArticleWithTagsList)
		blogRoute.POST("/article_with_tag_detail", blogHandler.GetArticleWithTagDetail)
		blogRoute.POST("/article_with_tags_list_for_private", blogHandler.GetArticleWithTagsListForPrivate)
		blogRoute.POST("/update_article", blogHandler.UpdateArticle)
		blogRoute.POST("/get_article_detail", blogHandler.GetArticleDetail)
		blogRoute.POST("/blog_settings_list", blogHandler.BlogSettingsList)
		blogRoute.POST("/update_blog_settings", blogHandler.UpdateBlogSettings)
		blogRoute.POST("/create_article", blogHandler.CreateArticle)
		blogRoute.POST("/get_hot_article", blogHandler.GetHotArticle)
	}

	// es blog
	esBlogRoute := route.Group("/es_blog")
	esBlogHandler := blogApi.NewEsBlogHandler(svc)
	{
		esBlogRoute.POST("/search", esBlogHandler.SearchEsBlogArticle)
	}

	// tag
	tagRoute := route.Group("/tag")
	tagHandler := blogApi.NewTagHandler(svc)
	{
		tagRoute.POST("/get_tag_list", tagHandler.GetTagList)
		tagRoute.POST("/create", tagHandler.CreateTag)
		tagRoute.POST("/get_tag_by_name", tagHandler.GetTagByName)
		tagRoute.POST("/update", tagHandler.UpdateTag)
	}
	// category
	categoryRoute := route.Group("/category")
	categoryHandler := blogApi.NewCategoryHandler(svc)
	{
		categoryRoute.POST("/get_category_list", categoryHandler.GetCategoryList)
		categoryRoute.POST("/create", categoryHandler.CreateCategory)
		categoryRoute.POST("/get_all_category", categoryHandler.GetAllCategory)
		categoryRoute.POST("/update", categoryHandler.UpdateCategory)
	}

	// test
	testApi := router.Group("/test")
	testHandler := handler.NewTestHandler(svc)
	{
		testApi.POST("/json", testHandler.TestJson)
		testApi.POST("/form", testHandler.TestFrom)
		testApi.POST("/params", testHandler.TestParams)
	}

	// WebSocket路由
	wsHandler := wsApi.NewWSHandler(svc)
	router.GET("/ws/connect", wsHandler.Connect) // WebSocket连接端点
	// 将消息发送API移出需要JWT验证的路由组
	router.POST("/ws/send", wsHandler.SendMessage) // 发送消息API

	// chat api
	chatRoute := route.Group("/chat")
	chatHandler := chat.NewChatHandler(svc)
	{
		chatRoute.POST("/get_messages", chatHandler.GetMessages)
		chatRoute.POST("/search_messages", chatHandler.SearchMessages)
		chatRoute.POST("/upload_file", chatHandler.UploadFile)
	}

	return router, svc
}
